Page({
    data: {
        header:{},
        userInfo:{},
        type:10,
        sex:0,
        item:'武汉',
        title:'',
        model:'',
        "num": 1,//数量
        "price": null,//价格
        "phone": "",//手机号
        "name": "",
        "gender": 1,
        "province": "请选择",
        "city": "请选择",
        "des": "",//描述
        linshiArr:[]
    },
     turnCity:function(){
      wx.navigateTo({
        url: '/pages/cityChoose/webView?type=5'
      })
    },
    onLoad: function(options) {
       var address= wx.getStorageSync('address');
        this.setData({
            address:address,
            city:address.city,
            item:address.city,
            province:address.province
        })
       var userInfo= wx.getStorageSync('userInfo');
        this.setData({
            userInfo:userInfo,
            name:userInfo.nickName,
            phone:userInfo.phone||''

        })
      getApp().getUserInfo().then(res=>{
      var header={
             'openid':res.openid, //公用请求头
             'content-type': 'application/json'
         }
       this.setData({
        userInfo:res,
        header:header
       })  

     })

    },
    onReady: function() {
        // 生命周期函数--监听页面初次渲染完成

    },
    onShow: function() {
     
       var chooseProvince= wx.getStorageSync('chooseProvince5');
       var chooseCity2=wx.getStorageSync('chooseCity5');
     
      
        if (chooseProvince) {
          this.setData({
            city:wx.getStorageSync('chooseCity5')||'全国',
            item:wx.getStorageSync('chooseCity5')||'全国',
            province:wx.getStorageSync('chooseProvince5')||'全国'
          })
        }
    

    },
    onHide: function() {
        // 生命周期函数--监听页面隐藏

    },
    onMyEvent:function(province){
      console.log(province.detail)
      this.setData({
        province:province.detail.split(',')[0],
        city:province.detail.split(',')[1]
      })
      
     
    },
    //输入标题
    textTitle:function (e) {
      console.log(e.detail.value);
      this.setData({
        title:e.detail.value
      })
    },
    //输入型号
    textModel:function (e) {
       this.setData({
        model:e.detail.value
      })
    },
    //输入价格
    textPrice:function (e) {
       this.setData({
        price:e.detail.value
      })
    },
    //输入电话
    textPhone:function (e) {
       this.setData({
        phone:e.detail.value
      })
    },
    //输入姓名
    textName:function (e) {
       this.setData({
        name:e.detail.value
      })
    },
    //输入描述
    textDes:function (e) {
       this.setData({
        des:e.detail.value
      })
    },
    //选中设备类型
    chooseType:function (e) {
        console.log('%***********')
        console.log(e.target.dataset.code)
        this.setData({
            type:e.target.dataset.code
        })
    },
    //选中性别类型
    chooseSex:function (e) {
        console.log('%***********')
        console.log(e.target.dataset.code)
        this.setData({
            gender:e.target.dataset.code
        })
    },
    //减
    subButton:function () {
      var num=this.data.num;
      num--;
      if (num<1) {
        num=1
      }
      this.setData({
        num:num
      })
    },
    //加
    addButton:function () {
        // body...
      var num=this.data.num;
      num++;
      if (num<1) {
        num==1
      }
      this.setData({
        num:num
      })
    },
    sendSheBei:function () {
      console.log(this.data.city)
         if (this.data.city==''||this.data.city=='请选择'||!this.data.city) {
            wx.showToast({
            title: '请选择城市',
            icon:'none',
            duration: 2000
          })
          return
         }
        if (this.data.title==''||this.data.name=='') {
          wx.showToast({
          title: '请填写完整信息',
          icon:'none',
          duration: 2000
        })
        return
        }
        if (this.data.phone.length<11) {
          wx.showToast({
          title: '请输入正确手机号',
          icon:'none',
          duration: 2000
        })
        return
        }
      if (this.data.linshiArr.length>6) {
             wx.showToast({
              title: '最多上传6张图片',
              icon:'none',
              duration: 2000
            })
             var  yarr=this.data.linshiArr.slice(0,6);

             this.setData({
               linshiArr:yarr
             })
      
       }
       console.log(this.data.linshiArr)
        var json={
            "openid":this.data.userInfo.openid,
            "type":this.data.type,
            "sex":0,
            "title":this.data.title,
            "model":this.data.model,
            "num": this.data.num,//数量
            "price": this.data.price||0,//价格
            "phone": this.data.phone,//手机号
            "name": this.data.name,
            "gender": this.data.gender,
            "province": this.data.province,
            "city": this.data.city,
            "des": this.data.des,//描述
            "wxEquipmentPic": this.data.linshiArr,
            "status":10

        }

        wx.request({
            url: getApp().globalData.baseUrl + '/wx/equipment/save',
            method:"POST",
            data:json,
            header: this.data.header,
            success: function(res) {
               console.log(res)
                wx.setStorageSync('chooseProvince5',null);
                wx.setStorageSync('chooseCity5',null);
                wx.switchTab({
                  url: '../../pages/mine/mine'
                })
                                    
            }
            })

    },chooseImage:function () {
      var _self=this;
      wx.chooseImage({
        success (res) {
          const tempFilePaths = res.tempFilePaths
          console.log(tempFilePaths[0])
          var imgUrl=tempFilePaths[0]
         
          console.log('##################')

         for (var i = tempFilePaths.length - 1; i >= 0; i--) {
              _self.uploadImg(tempFilePaths[i]);
              console.log(i)
          }

        
        }
      })
    },
    uploadImg:function (path) {
      var _self=this;
         wx.uploadFile({
            url:getApp().globalData.baseUrl+'/wx/img/url', //仅为示例，非真实的接口地址
            //url:'http://172.31.1.79:3000/users/uploadImg',
            filePath: path,
            header: {
              'content-type':'multipart/form-data',
              'openid':_self.data.userInfo.openid 
            },
            name:'filePath',
            formData: {
              openid:_self.data.userInfo.openid 
            },
            success (res){
            var data = JSON.parse(res.data);
             console.log(data.obj);
              var arr=_self.data.linshiArr;
              var json={url:data.obj}
              arr.push(json)
              
                _self.setData({
                    linshiArr:arr
                })
            }
          })
    },
    deleteImg:function (e) {
       console.log(e.target.dataset.code);
       var index=e.target.dataset.code;
       var arr=this.data.linshiArr;
       arr.splice(index,1);
       this.setData({
        linshiArr:arr
       })
    }

})